package travelDream.beans;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.interfaces.TransportInsertBeanLocal;


@ManagedBean(name="insertTransport")
@RequestScoped
public class TransportInsertManaged {

	private TransportationDTO tDTO;
	
	@EJB
	private TransportInsertBeanLocal transportInsert;
	
	public TransportInsertManaged()
	{
		tDTO=new TransportationDTO();	
	}
	
	public String insert()
	{
		if(!transportInsert.checkCity(tDTO)){
			if(transportInsert.checkDate(tDTO)){
				System.out.println("inserimento managed");
				transportInsert.insert(tDTO);
				return "transportationMean.xhtml?faces-redirect=true";
			}
			else{
				//notify the error
				 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "the dates don't match"));
			}
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "the cities don't match"));  
		}
		return "";
		
	}

	public TransportationDTO gettDTO() {
		return tDTO;
	}

	public void settDTO(TransportationDTO tDTO) {
		this.tDTO = tDTO;
	}
	
	
	
	
	
}
